//1. 引入路由和Vue
import Vue from "vue";
import VueRouter from "vue-router";

//2. 引入路由组件
const Home = () => import("../pages/Home");
const Login = () => import("../pages/Login");
const News = () => import("../pages/Home/News");
const Play = () => import("../pages/Home/Play");
const Music = () => import("../pages/Home/Music");
const MusicItem = () => import("../pages/Home/Music/Item");
const NewsItem = () => import("../pages/Home/News/Item");

//2. 使用VueRouter这个插件
Vue.use(VueRouter);

//重写push
//保留原始push
const oldPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (
  location,
  onComplete = () => {},
  onAbort = () => {}
) {
  oldPush.call(this, location, onComplete, onAbort);
};

//重写replace和重写push一致

//3. 创建路由对象
const router = new VueRouter({
  mode: "history",
  //路由表配置
  routes: [
    //初始路由重定向
    {
      path: "/",
      redirect: "/home",
    },
    {
      path: "/home",
      component: Home,
      // name: "home",
      //设置默认子路由-方式2
      // redirect: "/home/news",
      children: [
        //设置默认子路由-方式1
        {
          path: "",
          redirect: "news",
        },
        {
          path: "news",
          component: News,
          name: "news",
          children: [
            {
              path: "basketball/:name/:price?",
              name: "basketball",
              component: NewsItem,
            },
            {
              path: "money/:name/:price?",
              name: "money",
              component: NewsItem,
            },
            {
              path: "study/:name/:price?",
              name: "study",
              component: NewsItem,
            },
          ],
        },
        {
          path: "play",
          component: Play,
          name: "play",
        },
        {
          path: "music",
          component: Music,
          name: "music",
          children: [
            {
              path: "jay",
              name: "JAY",
              component: MusicItem,
            },
            {
              path: "jj",
              name: "JJ",
              component: MusicItem,
            },
            {
              path: "eson",
              name: "ESON",
              component: MusicItem,
            },
          ],
        },
      ],
    },
    {
      path: "/Login",
      component: Login,
      name: "login",
    },
  ],
});

//4. 把创建好的路由对象暴露出去
export default router;
